package test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import de.micromata.opengis.kml.v_2_2_0.Document;

import de.micromata.opengis.kml.v_2_2_0.Boundary;
import de.micromata.opengis.kml.v_2_2_0.Coordinate;
import de.micromata.opengis.kml.v_2_2_0.Feature;
import de.micromata.opengis.kml.v_2_2_0.Folder;
import de.micromata.opengis.kml.v_2_2_0.Geometry;
import de.micromata.opengis.kml.v_2_2_0.Kml;
import de.micromata.opengis.kml.v_2_2_0.LineString;
import de.micromata.opengis.kml.v_2_2_0.LineStyle;
import de.micromata.opengis.kml.v_2_2_0.LinearRing;
import de.micromata.opengis.kml.v_2_2_0.MultiGeometry;
import de.micromata.opengis.kml.v_2_2_0.Placemark;
import de.micromata.opengis.kml.v_2_2_0.Point;
import de.micromata.opengis.kml.v_2_2_0.PolyStyle;
import de.micromata.opengis.kml.v_2_2_0.Polygon;
import de.micromata.opengis.kml.v_2_2_0.Style;
import de.micromata.opengis.kml.v_2_2_0.StyleSelector;
/**
 * 
 * 创建人：Dream~天涯 
 * 创建时间：2015-8-6 上午11:38:00
 * 邮箱：154494796@qq.com
 * 备注：
 */
public class DemoKML2 {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws NoSuchFieldException, SecurityException{
		String geometryName;
		String color = null;
		String lineStylecolor = null;
		String polyStylecolor = null;
		Integer width = null;
		Integer lineStylewidth = null;
		List<KmlPoint> kmlPointList = new ArrayList<KmlPoint>();
		List<KmlLine> kmlLineList = new ArrayList<KmlLine>();
		List<KmlArea> kmlAreaList = new ArrayList<KmlArea>();
		KmlProperty kmlProperty = new KmlProperty();
		List<Geometry> polygonList = null;
		List<Coordinate> coordinates;
		MultiGeometry multiGeometry = null;
		LineString lineString = null;
		LinearRing linearRing = null;
		Point point = null;
		final Kml kml = Kml.unmarshal(new File("D://x.kml"));
		Document document = (Document) kml.getFeature();
		List<Feature> featureList = document.getFeature();
		for (int n = 0; n < featureList.size(); n++) {
			Feature featureStyle = document.getFeature().get(n);
			String featureNamess = featureStyle.toString().split("@")[0];
			String[] featureNames = featureNamess.split("\\.");
			String featureName = featureNames[featureNames.length-1];
			if ("Folder".equals(featureName)) {
				Folder folder = (Folder) document.getFeature().get(n);
				if("Contours-app.grd".equals(folder.getName())){
					int folderSize = folder.getFeature().size();
					for (int i = 0; i < folderSize; i++) {
						Placemark placemark = (Placemark) folder.getFeature().get(i);
						List<StyleSelector> style = placemark.getStyleSelector();
						for(int m=0 ; m<style.size() ; m++){
							Style s=(Style) style.get(m);
							PolyStyle polyStyle=s.getPolyStyle();
							LineStyle lineStyle = s.getLineStyle();
							if(polyStyle==null){
								color = lineStyle.getColor();
								width = (int) lineStyle.getWidth();
							}else{
								color = polyStyle.getColor();
							}
						}
						geometryName = geometryName(placemark);
						if("MultiGeometry".equals(geometryName)){
							multiGeometry = (MultiGeometry)  placemark.getGeometry();
							polygonList = multiGeometry.getGeometry();
							for(int j=0 ; j<polygonList.size() ; j++){
								KmlArea kmlArea = new KmlArea();
								Boundary outerBoundaryIs = ((Polygon) polygonList.get(j)).getOuterBoundaryIs();
								linearRing = outerBoundaryIs.getLinearRing();//面
								coordinates = linearRing.getCoordinates();
								//System.out.println(coordinates);
								kmlArea.setPoints(coordinates.toString());
								kmlArea.setAreaColor(color);
								kmlAreaList.add(kmlArea);
								kmlProperty.setKmlAreas(kmlAreaList);
							}
						}else if("LineString".equals(geometryName)){//线
							KmlLine kmlLine = new KmlLine();
							lineString = (LineString)  placemark.getGeometry();
							coordinates = lineString.getCoordinates();
							//System.out.println(coordinates);
							kmlLine.setColor(color);
							kmlLine.setPoints(coordinates.toString());
							kmlLine.setWidth(width);
							kmlLineList.add(kmlLine);
							kmlProperty.setKmlLines(kmlLineList);
						}else if("Point".equals(geometryName)){//点
							KmlPoint kmlPoint = new KmlPoint();
							point = (Point)  placemark.getGeometry();
							coordinates = point.getCoordinates();
							//System.out.println(coordinates);
							kmlPoint.setPointVal(Double.valueOf(placemark.getName()));
							kmlPoint.setColor(color);
							kmlPoint.setPoints(coordinates.toString());
							kmlPointList.add(kmlPoint);
							kmlProperty.setKmlPoints(kmlPointList);
						}else if("Polygon".equals(geometryName)){
							polygonList = (List<Geometry>)  placemark.getGeometry();
							for(int j=0 ; j<polygonList.size() ; j++){
								Boundary outerBoundaryIs = ((Polygon) polygonList.get(j)).getOuterBoundaryIs();
								linearRing = outerBoundaryIs.getLinearRing();
								coordinates = linearRing.getCoordinates();
							}
						}else{
							System.out.println("其他类型，请联系开发者补充..........................");
						}
					}
				}
			}else if("Placemark".equals(featureName)){//此段代码暂时用不上，注释，误删
				Placemark placemark = (Placemark) document.getFeature().get(n);
				geometryName = geometryName(placemark);
				if("Point".equals(geometryName)){
					KmlPoint kmlPoint = new KmlPoint();
					point = (Point)  placemark.getGeometry();
					coordinates = point.getCoordinates();
					kmlPoint.setPointVal(Double.valueOf(placemark.getName()));
					kmlPoint.setColor(color);
					kmlPoint.setPoints(coordinates.toString());
					kmlPointList.add(kmlPoint);
					kmlProperty.setKmlPoints(kmlPointList);
				}else if("Polygon".endsWith(geometryName)){
					List<StyleSelector> style = placemark.getStyleSelector();
					for(int m=0 ; m<style.size() ; m++){
						Style s=(Style) style.get(m);
						PolyStyle polyStyle=s.getPolyStyle();
						LineStyle lineStyle=s.getLineStyle();
						lineStylecolor = lineStyle.getColor();
						lineStylewidth = (int) lineStyle.getWidth();
						polyStylecolor = polyStyle.getColor();
					}

					KmlArea kmlArea = new KmlArea();
					Polygon polygon = (Polygon) placemark.getGeometry();
					Boundary boundary = polygon.getOuterBoundaryIs();
					linearRing = boundary.getLinearRing();
					coordinates = linearRing.getCoordinates();
					kmlArea.setPoints(coordinates.toString());
					kmlArea.setAreaColor(polyStylecolor);
					kmlArea.setOutLineColor(lineStylecolor);
					kmlAreaList.add(kmlArea);
					kmlProperty.setKmlAreas(kmlAreaList);
					//System.out.println(linearRing.getCoordinates());
					//System.out.println(placemark.getName());
				}else{
					System.out.println();
				}
			}else{
				System.err.println("其他类型，请联系开发者添加该类型！");
			}
		}

		System.out.println();
	}

	public static String geometryName(Placemark placemark) {
		String geometryNamess = placemark.getGeometry().toString().split("@")[0];
		String[] geometryNames = geometryNamess.split("\\.");
		String geometryName = geometryNames[geometryNames.length-1];
		return geometryName;
	}
}
